#!/usr/bin/env python

# Copyright (c) 2009, Purdue University
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
# 
# Neither the name of the Purdue University nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Remove views and dns server set view assignments for roster"""


__copyright__ = 'Copyright (C) 2009, Purdue University'
__license__ = 'BSD'
__version__ = '0.17'


import sys

from roster_user_tools import cli_common_lib
from roster_user_tools import roster_client_lib
from roster_user_tools.action_flags import Remove
from roster_user_tools.data_flags import View


class Args(Remove, View):
  pass


def main(args):
  """Collects command line arguments.

  Inputs:
    args: list of arguments from the command line
  """
  command = None
  if( args and not args[0].startswith('-') ):
    command = args.pop(0)
  usage = ('\n'
           '\n'
           'To remove views:\n'
           '\t%s view -v <view-name> [-o <options>]\n'
           '\n'
           'To remove dns server set view assignments:\n'
           '\t%s dns_server_set -v <view-name> -e <dns-server-set>\n'
           '\n'
           'To remove acl view assignments:\n'
           '\t%s acl -v <view-name> -a <acl-name> [--allow/--deny]\n'
           '\n'
           'To remove view assignments:\n'
           '\t%s view_subset -v <view-superset-name>\n'
           '\t-V <view-subset-name>\n' % tuple(
               [sys.argv[0] for _ in range(4)]))

  args_instance = Args(command,
      ['view', 'dns_server_set', 'acl', 'view_subset'], args, usage)
  options = args_instance.options

  try:
    cli_common_lib_instance = cli_common_lib.CliCommonLib(options)
  except cli_common_lib.ArgumentError, error:
    cli_common_lib.DnsError(str(error), 1)

  views = roster_client_lib.RunFunction(
      u'ListViews', options.username, credfile=options.credfile,
      credstring=options.credstring, server_name=options.server,
      kwargs={u'view_name': options.view_name})['core_return']

  if( command == 'view' ):
    if( options.view_name not in views ):
      cli_common_lib.DnsError(
          'View "%s" does not exist.' % options.view_name, 1)

    roster_client_lib.RunFunction(
        u'RemoveView', options.username, credfile=options.credfile,
        credstring=options.credstring, server_name=options.server,
        args=[options.view_name])

    if( not options.quiet ):
      print 'REMOVED VIEW view_name: view_name: %s options %s' % (
          options.view_name, options.options)

  elif( command == 'dns_server_set' ):
    if( options.view_name not in views ):
      cli_common_lib.DnsError(
          'View "%s" does not exist.' % options.view_name, 1)
    dns_server_sets = roster_client_lib.RunFunction(
        u'ListDnsServerSets', options.username,
        credfile=options.credfile, credstring=options.credstring,
        server_name=options.server)['core_return']
    if( not options.dns_server_set in dns_server_sets ):
      cli_common_lib.DnsError(
          'Dns Server Set "%s" does not exist.' % options.dns_server_set, 1)
    roster_client_lib.RunFunction(
        u'RemoveDnsServerSetViewAssignments', options.username,
        credfile=options.credfile, credstring=options.credstring,
        server_name=options.server,
        args=[options.view_name, options.dns_server_set])
    if( not options.quiet ):
      print ('REMOVED DNS SERVER SET VIEW ASSIGNMENT: view_name: %s '
             'dns_server_set: %s' % (options.view_name, options.dns_server_set))

  elif( command == 'acl' ):
    if( options.view_name not in views ):
      cli_common_lib.DnsError(
          'View "%s" does not exist.' % options.view_name, 1)
    if( options.allow is None ):
      cli_common_lib.UserError('Either --allow or --deny must be used.', 1)
    view_acl_assignments = roster_client_lib.RunFunction(
        u'ListViewToACLAssignments', options.username,
        credfile=options.credfile, credstring=options.credstring,
        server_name=options.server,
        kwargs={'view_name': options.view_name, u'acl_name': options.acl})[
            'core_return']
    if( not view_acl_assignments ):
      cli_common_lib.DnsError('View ACL Assignment does not exist.', 1)

    allowed = 0
    if( options.allow == 1 ):
      allowed = 1

    roster_client_lib.RunFunction(
        u'RemoveViewToACLAssignments', options.username,
        credfile=options.credfile, credstring=options.credstring,
        server_name=options.server, args=[options.view_name, options.acl,
                                          allowed])
    if( not options.quiet ):
      print 'REMOVED VIEW TO ACL ASSIGNMENT: view_name: %s acl_name: %s' % (
          options.view_name, options.acl)

  elif( command == 'view_subset' ):
    if( options.view_name not in views ):
      cli_common_lib.DnsError(
          'View "%s" does not exist.' % options.view_name, 1)
    view_assignments = roster_client_lib.RunFunction(
        u'ListViewAssignments', options.username,
        credfile=options.credfile, credstring=options.credstring,
        server_name=options.server,
        kwargs={'view_superset': options.view_name,
                'view_subset': options.view_subset})['core_return']
    view_assignment_exists = True
    if( options.view_name not in view_assignments ):
      view_assignment_exists = False
    elif( options.view_subset not in view_assignments[options.view_name] ):
      view_assignment_exists = False

    if( not view_assignment_exists ):
      cli_common_lib.DnsError('View assignment does not exist.', 1)
    else:
      roster_client_lib.RunFunction(
          u'RemoveViewAssignment', options.username,
          credfile=options.credfile, credstring=options.credstring,
          server_name=options.server,
          args=[options.view_name, options.view_subset])
      if( not options.quiet ):
        print 'REMOVED VIEW ASSIGNMENT: view_name: %s view_subset: %s' % (
            options.view_name, options.view_subset)
  else:
    cli_common_lib.DnsError(
        'Command %s exists, but codepath doesn\'t.' % command, 1)

if __name__ == "__main__":
  main(sys.argv[1:])
